package com.sinochem.yunlian.ship.base.rpc;

import com.sinochem.yunlian.ship.api.vo.bs.BsPortThridMappingInfo;
import com.sinochem.yunlian.ship.api.vo.bs.BsTideThridPortInfo;
import com.sinochem.yunlian.ship.api.vo.bs.PortVO;
import com.sinochem.yunlian.ship.api.vo.bs.TideInfoForm;
import com.sinochem.yunlian.ship.base.biz.BsPortBiz;
import com.sinochem.yunlian.ship.base.biz.BsPortThridMappingBiz;
import com.sinochem.yunlian.ship.base.biz.BsTideThridPortBiz;
import com.sinochem.yunlian.ship.base.constants.BsPortConstant;
import com.sinochem.yunlian.ship.base.entity.BsPort;
import com.sinochem.yunlian.ship.base.entity.BsPortThridMapping;
import com.sinochem.yunlian.ship.base.entity.BsTideThridPort;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@IgnoreToken
@RequestMapping("api/port")
public class BsPortRest {
	
	@Resource
	private BsPortBiz bsPortBiz;
	@Autowired
	private BsPortThridMappingBiz portThridMappingBiz;
	@Autowired
	private BsTideThridPortBiz tideThridPortBiz;
	
	/**
	 *
	 * @Descript 根据港口id获取港口名称
	 *
	 * @Date 2017年12月8日
	 */
	@RequestMapping(value = "getPortName/{id}", method = RequestMethod.GET)
	public String getPortName(@PathVariable("id") Long id) {
		return bsPortBiz.getPortNameById(id);
	}

	@RequestMapping(value = "/findPortListByName/{portName}", method = RequestMethod.GET)
	public List<PortVO> findPortListByName(@RequestParam("portName") String portName) {
		List<PortVO> portVOList = new ArrayList<PortVO>();
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("level",BsPortConstant.LEVLE_3);
		params.put("disableStatus",BsPortConstant.STATUS_ENABLE);
		params.put("portName",portName);
		List<BsPort> bsPortList = bsPortBiz.findPortListByMap(params);
		for(BsPort bsPort : bsPortList) {
			PortVO portVO = new PortVO();
			portVO.setId(bsPort.getId());
			portVO.setPortCode(bsPort.getPortCode());
			portVO.setPortName(bsPort.getPortName());
			portVO.setLevel(bsPort.getLevel());
			portVO.setParentId(bsPort.getParentId());
			portVO.setLat(bsPort.getLat());
			portVO.setLon(bsPort.getLon());
			portVOList.add(portVO);
		}
		return portVOList;
	}

	@RequestMapping(value = "/findByPortId", method = RequestMethod.GET)
	public PortVO findByPortId(@RequestParam("portId") Long portId) {
		PortVO portVO = null;
		BsPort bsPort = bsPortBiz.selectById(portId);
		if (bsPort != null ) {
			if (bsPort.getDisableStatus() == BsPortConstant.STATUS_DISABLE) {
				return null;
			}
			portVO = new PortVO();
			portVO.setId(bsPort.getId());
			portVO.setPortCode(bsPort.getPortCode());
			portVO.setPortName(bsPort.getPortName());
			portVO.setLevel(bsPort.getLevel());
			portVO.setParentId(bsPort.getParentId());
			portVO.setLat(bsPort.getLat());
			portVO.setLon(bsPort.getLon());
		}
		return portVO;
	}

	/**
	 * 根据第三方港口ID 查询港口信息列表
	 * @param thridPortId
	 * @return
	 */
	@RequestMapping(value = "/getPortMappingByThridPortId/{id}", method = RequestMethod.GET)
	public List<BsPortThridMappingInfo> getPortMappingByThridPortId(@PathVariable("id") Long thridPortId){
        if(thridPortId == null){
        	return null;
		}

		Map inMap = new HashMap();
        inMap.put("thridPortId",thridPortId);
		List<BsPortThridMapping> bsPortThridMappings =  portThridMappingBiz.findByCondition(inMap);
        if(bsPortThridMappings == null || bsPortThridMappings.isEmpty()){
        	return null;
		}
		List<BsPortThridMappingInfo> bsPortThridMappingInfos = new ArrayList<>();
        for(BsPortThridMapping bsPortThridMapping : bsPortThridMappings){
			BsPortThridMappingInfo bsPortThridMappingInfo = new BsPortThridMappingInfo();
			BeanUtils.copyProperties(bsPortThridMapping,bsPortThridMappingInfo);
			bsPortThridMappingInfos.add(bsPortThridMappingInfo);
		}
		return  	bsPortThridMappingInfos;
	}

	/**
	 * 返回 出发港和目的港的portId
	 * @param portName
	 * @return
	 */
	@RequestMapping(value = "/getPortMappingByThridPortName", method = RequestMethod.POST)
	public Map<String,Object> getPortMappingByThridPortName(@RequestBody Map portName){
		Map<String,Object> result = null;
		String startPortName = portName.get("startPort")+"";
		String endPortName = portName.get("endPort")+"";

		Map inMap = new HashMap();
		inMap.put("thridPortName",startPortName);
		List<BsPortThridMapping> startPortMapper = null;
		List<BsPortThridMapping> endPortMapper = null;
		startPortMapper = portThridMappingBiz.findByCondition(inMap);
		inMap.put("thridPortName",endPortName);
		endPortMapper = portThridMappingBiz.findByCondition(inMap);

		if(isNotEmpty(startPortMapper, endPortMapper)){
			result= new HashedMap();
			result.put("startPort",startPortMapper.get(0).getPortId());
			result.put("endPort",endPortMapper.get(0).getPortId());
		}
		return  result;
	}

	private boolean isNotEmpty(List<BsPortThridMapping> startPortMapper, List<BsPortThridMapping> endPortMapper) {
		return (startPortMapper!=null&&startPortMapper.size()>0)&&(endPortMapper!=null&&endPortMapper.size()>0);
	}


	/**
	 *   根据条件查询对应的潮汐观察点
	 * @return
	 */
	@RequestMapping(value = "findTideInfos",method = RequestMethod.POST)
	public List<BsTideThridPortInfo> findTideInfos(@RequestBody TideInfoForm tideInfoForm){
		Map inMap = new HashMap();
		inMap.put("leftLongitude",tideInfoForm.getLeftLongitude());
		inMap.put("rightLongitude",tideInfoForm.getRightLongitude());
		inMap.put("topLatitude",tideInfoForm.getTopLatitude());
		inMap.put("downLatitude",tideInfoForm.getDownLatitude());
		List<BsTideThridPort> tideThridPorts = tideThridPortBiz.findTideByCondition(inMap);
		if(tideThridPorts == null || tideThridPorts.isEmpty()){
			return null;
		}
		List<BsTideThridPortInfo> tideThridPortInfos = new ArrayList<>();
		for(BsTideThridPort tideThridPort : tideThridPorts){
			BsTideThridPortInfo  tideThridPortInfo = new BsTideThridPortInfo();
			BeanUtils.copyProperties(tideThridPort,tideThridPortInfo);
			tideThridPortInfo.setTideId(tideThridPort.getPortId());
			tideThridPortInfos.add(tideThridPortInfo);
		}
		return tideThridPortInfos;
	}


	/**
	 * 获取潮汐观测点
	 * @return
	 */
	@RequestMapping(value = "getTideThridPort",method = RequestMethod.POST)
	public List<BsTideThridPortInfo>  getTideThridPort(){
		List<BsTideThridPort> tideThridPorts = tideThridPortBiz.findTideByCondition(null);
		if(tideThridPorts == null || tideThridPorts.isEmpty()){
			return null;
		}
		List<BsTideThridPortInfo> tideThridPortInfos = new ArrayList<>();
		for(BsTideThridPort tideThridPort : tideThridPorts){
			BsTideThridPortInfo  tideThridPortInfo = new BsTideThridPortInfo();
			BeanUtils.copyProperties(tideThridPort,tideThridPortInfo);
			tideThridPortInfo.setTideId(tideThridPort.getPortId());
			tideThridPortInfos.add(tideThridPortInfo);
		}
		return tideThridPortInfos;
	}



}